﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;

namespace PhineClient
{
    public partial class MainPage : PhoneApplicationPage
    {
        private Client client = new Client();
        private string url = @"http://localhost:8811/";
        private string Nickname = null;
        private string Separator = "^";
        private Thread CheckThread = null;
        private bool flag = true;

        // Конструктор
        public MainPage()
        {
            InitializeComponent();
        }

        private void Login_btn_Click(object sender, RoutedEventArgs e)
        {
            Nickname = LoginBox.Text;
            string message = string.Format("{0}{1}{2}", "Login", Separator, Nickname);
            client.connect(ref message, url);

            CheckThread = new Thread(CheckTimer);
            CheckThread.Start();
            
        }

        private void CheckTimer()
        {
            while (flag)
            {               
                Deployment.Current.Dispatcher.BeginInvoke(()=>
                {
                    string message = string.Format("{0}{1}{2}", "Check", Separator, Nickname);
                    client.connect(ref message, url);

                    string[] Listofplayers = client.Response.Split('^');
                    ListPlayers.Items.Clear();
                    foreach (string str in Listofplayers)
                    {
                        ListPlayers.Items.Add(str);
                    }                          
                });
                Thread.Sleep(2000);                 
            }
        }
    }
}